package com.hsjk.szwj.module.pay.service;

import com.hsjk.szwj.module.pay.dal.dataobject.config.ChannelRetMsg;
import com.hsjk.szwj.module.pay.dal.dataobject.order.RefundOrderDO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author chenjiawei
 * @desc
 * @Date 2023/08/25 01:03
 **/
@Service
@Slf4j
public class RefundOrderProcessService {
    @Resource
    private RefundOrderService refundOrderService;
    @Resource
    private PayMchNotifyService payMchNotifyService;

    /**
     * 根据通道返回的状态，处理退款订单业务
     **/
    public boolean handleRefundOrder4Channel(ChannelRetMsg channelRetMsg,
                                             RefundOrderDO refundOrder) {
        //默认更新成功
        boolean updateOrderSuccess = true;
        Long refundOrderId = refundOrder.getId();
        // 明确退款成功
        if (channelRetMsg.getChannelState() == ChannelRetMsg.ChannelState.CONFIRM_SUCCESS) {
            updateOrderSuccess = refundOrderService.updateIng2Success(refundOrderId, channelRetMsg.getChannelOrderId());
            if (updateOrderSuccess) {
                // 通知商户系统
                if (StringUtils.isNotEmpty(refundOrder.getNotifyUrl())) {
                    payMchNotifyService.refundOrderNotify(refundOrderService.getById(refundOrderId));
                }
            }
            //确认失败
        } else if (channelRetMsg.getChannelState() == ChannelRetMsg.ChannelState.CONFIRM_FAIL) {
            // 更新为失败状态
            updateOrderSuccess = refundOrderService.updateIng2Fail(refundOrderId, channelRetMsg.getChannelOrderId(), channelRetMsg.getChannelErrCode(), channelRetMsg.getChannelErrMsg());
            // 通知商户系统
            if (StringUtils.isNotEmpty(refundOrder.getNotifyUrl())) {
                payMchNotifyService.refundOrderNotify(refundOrderService.getById(refundOrderId));
            }
        }
        return updateOrderSuccess;
    }
}
